<template>
    <div class="app-container">
        <div class="remark">
            <div>使用：将“中华人民共和国民政部”行政区划数据直接粘贴到左侧文本域，点击“开始”按钮，即可生成省市区三级数据并展示在右侧文本域中。</div>
        </div>
        
        <div class="row-flex jb">
            <div class="source-data"><el-input type="textarea" v-model="originData" rows="9" placeholder="请将需要处理的省市区数据粘贴到此处"></el-input></div>
            <div class="result-data"><el-input type="textarea" v-model="jsonStr" rows="9"></el-input></div>
        </div>
        <div class="">
            
        </div>
        <div class="operation">
            <el-button type="primary" @click="handlerEl">开始</el-button>
            <el-button type="primary" @click="handlerFilterStr">转字符串</el-button>
            <el-button type="primary" @click="handlerFilterSpace">过滤空格</el-button>
            <el-button type="primary" @click="handlerFilterTab">过滤tab符号</el-button>
        </div>
        
        <div class="remark">
            <div>注：台湾省、香港特别行政区和澳门特别行政区暂缺地市和区县信息（来自民政部官网）</div>
            <div>说明：由于民政部官网暂缺港澳台的地市和区县信息，所以这里提供的港澳台地市和区县信息可能不够准确。</div>
        </div>
        <div>
            <el-cascader
            v-model="activeArea"
            :props="propsObj"
            :options="resultData"
            @change="handleChange"></el-cascader>
            <div>当前选中：<template v-if="activeArea">{{activeArea[0]}}/{{activeArea[1]}}/{{activeArea[2]}}</template></div>
        </div>
    </div>
</template>
<script>
/**
 * 本组件的目的是根据"中华人民共和共民政部"数据快速生成省市区三级数据
 * 由于民政部官网每年都会更新行政区划信息，而作者在网上找到的现成省市区json文件又是比较旧的数据，所以开发本组件的目的是为了根据民政部每年更新的行政区划信息来快速生成省市区三级json文件。
 * 
 * 使用：将民政部的数据直接粘贴到第一个文本域，点击开始按钮 (http://www.mca.gov.cn/article/sj/xzqh/2020/20201201.html)
 * 注意行政代码规则不一致的地方：
 *      1.重庆的区和县第4位代码不同
 *      2.新疆下面的xx市、xx自治州、xx地区的第3位代码不同
 *      3.内蒙古下面的xx市、xx盟第3位代码不同
 *      4.新疆阿勒泰地区市县第3位代码不同
 * 
 * 未处理的地方：
 *      1.有些市下面没有区和县，只有街道/镇，民政部官网提供的数据里不带镇和街道（只有县及以上的数据或县以下行政区划变更数据），所以就导致有些市下面的数据缺失，比如：东莞市、中山市，这些市的下一级就只能自己去查资料然后手动补全。这个问题就导致需要检查所有的市下辖的镇或街道到底有没有缺失，这个好像无法避免。目前能想到的解决方案是：列出所有市/xx自治州/xx地区/xx盟 的下级区/县的总数，然后挨个城市的查阅资料，比如百科能查到“截至2020年6月，东莞市下辖4个街道、28个镇”，首先看总数，如果有缺少补上。如果总数没变，但xx镇/xx乡变撤销变成了新设立xx街道或者名称变更等等，这个没法从总数看出来，依然需要大量时间对比。甚至东莞市、中山市下面一个区县都没有，只有街道、镇，这样最终的数据就导致第三级区县是空的，这就导致用户没法选择。
 *      解决方案：目前我能想到的解决方案就是，如果第三级没有区县那就把市作为三级放上去，目的就是为了能选择，具体的街道和镇就让用户手动填写到详细地址中，这也是最方便快捷的办法了，否则一个个去找街道/镇，以及镇更名，新设立镇这些时间成本太高。
 *      
 *      2.乡/镇/街道的行政区划代码是9位数字
 * 
 *      2.有些区县没有行政区划代码，也要自己手动补全，比如：三沙市
 * 
 * 数据生成完毕需要检查的几个地区: 重庆的区和县是否完整、新疆的xx市、xx自治州、xx地区是否完整 (目前看这几个地方是没问题的)
 * 
 */
export default {
    name: "area",
    data(){
        return {
            originData: '',
            activeArea: [],
            propsObj: {
                value: 'label',
                label: 'label'
            },
            resultData: [], // 处理完成的数据 三级
            jsonStr: '',
            otherArea: true,
            otherData: [
                {
                    "children": [
                        {
                            "label": "台北市",
                            "children": [
                                {
                                    "id": 710101,
                                    "label": "松山区"
                                },
                                {
                                    "id": 710102,
                                    "label": "信义区"
                                },
                                {
                                    "id": 710103,
                                    "label": "大安区"
                                },
                                {
                                    "id": 710104,
                                    "label": "中山区"
                                },
                                {
                                    "id": 710105,
                                    "label": "中正区"
                                },
                                {
                                    "id": 710106,
                                    "label": "大同区"
                                },
                                {
                                    "id": 710107,
                                    "label": "万华区"
                                },
                                {
                                    "id": 710108,
                                    "label": "文山区"
                                },
                                {
                                    "id": 710109,
                                    "label": "南港区"
                                },
                                {
                                    "id": 710110,
                                    "label": "内湖区"
                                },
                                {
                                    "id": 710111,
                                    "label": "士林区"
                                },
                                {
                                    "id": 710112,
                                    "label": "北投区"
                                }
                            ],
                            "id": 710100
                        },
                        {
                            "label": "高雄市",
                            "children": [
                                {
                                    "id": 710201,
                                    "label": "盐埕区"
                                },
                                {
                                    "id": 710202,
                                    "label": "鼓山区"
                                },
                                {
                                    "id": 710203,
                                    "label": "左营区"
                                },
                                {
                                    "id": 710204,
                                    "label": "楠梓区"
                                },
                                {
                                    "id": 710205,
                                    "label": "三民区"
                                },
                                {
                                    "id": 710206,
                                    "label": "新兴区"
                                },
                                {
                                    "id": 710207,
                                    "label": "前金区"
                                },
                                {
                                    "id": 710208,
                                    "label": "苓雅区"
                                },
                                {
                                    "id": 710209,
                                    "label": "前镇区"
                                },
                                {
                                    "id": 710210,
                                    "label": "旗津区"
                                },
                                {
                                    "id": 710211,
                                    "label": "小港区"
                                },
                                {
                                    "id": 710212,
                                    "label": "凤山区"
                                },
                                {
                                    "id": 710213,
                                    "label": "林园区"
                                },
                                {
                                    "id": 710214,
                                    "label": "大寮区"
                                },
                                {
                                    "id": 710215,
                                    "label": "大树区"
                                },
                                {
                                    "id": 710216,
                                    "label": "大社区"
                                },
                                {
                                    "id": 710217,
                                    "label": "仁武区"
                                },
                                {
                                    "id": 710218,
                                    "label": "鸟松区"
                                },
                                {
                                    "id": 710219,
                                    "label": "冈山区"
                                },
                                {
                                    "id": 710220,
                                    "label": "桥头区"
                                },
                                {
                                    "id": 710221,
                                    "label": "燕巢区"
                                },
                                {
                                    "id": 710222,
                                    "label": "田寮区"
                                },
                                {
                                    "id": 710223,
                                    "label": "阿莲区"
                                },
                                {
                                    "id": 710224,
                                    "label": "路竹区"
                                },
                                {
                                    "id": 710225,
                                    "label": "湖内区"
                                },
                                {
                                    "id": 710226,
                                    "label": "茄萣区"
                                },
                                {
                                    "id": 710227,
                                    "label": "永安区"
                                },
                                {
                                    "id": 710228,
                                    "label": "弥陀区"
                                },
                                {
                                    "id": 710229,
                                    "label": "梓官区"
                                },
                                {
                                    "id": 710230,
                                    "label": "旗山区"
                                },
                                {
                                    "id": 710231,
                                    "label": "美浓区"
                                },
                                {
                                    "id": 710232,
                                    "label": "六龟区"
                                },
                                {
                                    "id": 710233,
                                    "label": "甲仙区"
                                },
                                {
                                    "id": 710234,
                                    "label": "杉林区"
                                },
                                {
                                    "id": 710235,
                                    "label": "内门区"
                                },
                                {
                                    "id": 710236,
                                    "label": "茂林区"
                                },
                                {
                                    "id": 710237,
                                    "label": "桃源区"
                                },
                                {
                                    "id": 710238,
                                    "label": "那玛夏区"
                                }
                            ],
                            "id": 710200
                        },
                        {
                            "label": "台南市",
                            "children": [
                                {
                                    "id": 710501,
                                    "label": "东区"
                                },
                                {
                                    "id": 710502,
                                    "label": "南区"
                                },
                                {
                                    "id": 710504,
                                    "label": "北区"
                                },
                                {
                                    "id": 710506,
                                    "label": "安南区"
                                },
                                {
                                    "id": 710507,
                                    "label": "安平区"
                                },
                                {
                                    "id": 710508,
                                    "label": "中西区"
                                },
                                {
                                    "id": 710509,
                                    "label": "新营区"
                                },
                                {
                                    "id": 710510,
                                    "label": "盐水区"
                                },
                                {
                                    "id": 710511,
                                    "label": "白河区"
                                },
                                {
                                    "id": 710512,
                                    "label": "柳营区"
                                },
                                {
                                    "id": 710513,
                                    "label": "后壁区"
                                },
                                {
                                    "id": 710514,
                                    "label": "东山区"
                                },
                                {
                                    "id": 710515,
                                    "label": "麻豆区"
                                },
                                {
                                    "id": 710516,
                                    "label": "下营区"
                                },
                                {
                                    "id": 710517,
                                    "label": "六甲区"
                                },
                                {
                                    "id": 710518,
                                    "label": "官田区"
                                },
                                {
                                    "id": 710519,
                                    "label": "大内区"
                                },
                                {
                                    "id": 710520,
                                    "label": "佳里区"
                                },
                                {
                                    "id": 710521,
                                    "label": "学甲区"
                                },
                                {
                                    "id": 710522,
                                    "label": "西港区"
                                },
                                {
                                    "id": 710523,
                                    "label": "七股区"
                                },
                                {
                                    "id": 710524,
                                    "label": "将军区"
                                },
                                {
                                    "id": 710525,
                                    "label": "北门区"
                                },
                                {
                                    "id": 710526,
                                    "label": "新化区"
                                },
                                {
                                    "id": 710527,
                                    "label": "善化区"
                                },
                                {
                                    "id": 710528,
                                    "label": "新市区"
                                },
                                {
                                    "id": 710529,
                                    "label": "安定区"
                                },
                                {
                                    "id": 710530,
                                    "label": "山上区"
                                },
                                {
                                    "id": 710531,
                                    "label": "玉井区"
                                },
                                {
                                    "id": 710532,
                                    "label": "楠西区"
                                },
                                {
                                    "id": 710533,
                                    "label": "南化区"
                                },
                                {
                                    "id": 710534,
                                    "label": "左镇区"
                                },
                                {
                                    "id": 710535,
                                    "label": "仁德区"
                                },
                                {
                                    "id": 710536,
                                    "label": "归仁区"
                                },
                                {
                                    "id": 710537,
                                    "label": "关庙区"
                                },
                                {
                                    "id": 710538,
                                    "label": "龙崎区"
                                },
                                {
                                    "id": 710539,
                                    "label": "永康区"
                                }
                            ],
                            "id": 710500
                        },
                        {
                            "label": "台中市",
                            "children": [
                                {
                                    "id": 710401,
                                    "label": "中区"
                                },
                                {
                                    "id": 710402,
                                    "label": "东区"
                                },
                                {
                                    "id": 710403,
                                    "label": "南区"
                                },
                                {
                                    "id": 710404,
                                    "label": "西区"
                                },
                                {
                                    "id": 710405,
                                    "label": "北区"
                                },
                                {
                                    "id": 710406,
                                    "label": "西屯区"
                                },
                                {
                                    "id": 710407,
                                    "label": "南屯区"
                                },
                                {
                                    "id": 710408,
                                    "label": "北屯区"
                                },
                                {
                                    "id": 710409,
                                    "label": "丰原区"
                                },
                                {
                                    "id": 710410,
                                    "label": "东势区"
                                },
                                {
                                    "id": 710411,
                                    "label": "大甲区"
                                },
                                {
                                    "id": 710412,
                                    "label": "清水区"
                                },
                                {
                                    "id": 710413,
                                    "label": "沙鹿区"
                                },
                                {
                                    "id": 710414,
                                    "label": "梧栖区"
                                },
                                {
                                    "id": 710415,
                                    "label": "后里区"
                                },
                                {
                                    "id": 710416,
                                    "label": "神冈区"
                                },
                                {
                                    "id": 710417,
                                    "label": "潭子区"
                                },
                                {
                                    "id": 710418,
                                    "label": "大雅区"
                                },
                                {
                                    "id": 710419,
                                    "label": "新社区"
                                },
                                {
                                    "id": 710420,
                                    "label": "石冈区"
                                },
                                {
                                    "id": 710421,
                                    "label": "外埔区"
                                },
                                {
                                    "id": 710422,
                                    "label": "大安区"
                                },
                                {
                                    "id": 710423,
                                    "label": "乌日区"
                                },
                                {
                                    "id": 710424,
                                    "label": "大肚区"
                                },
                                {
                                    "id": 710425,
                                    "label": "龙井区"
                                },
                                {
                                    "id": 710426,
                                    "label": "雾峰区"
                                },
                                {
                                    "id": 710427,
                                    "label": "太平区"
                                },
                                {
                                    "id": 710428,
                                    "label": "大里区"
                                },
                                {
                                    "id": 710429,
                                    "label": "和平区"
                                }
                            ],
                            "id": 710400
                        },
                        {
                            "label": "南投县",
                            "children": [
                                {
                                    "id": 712801,
                                    "label": "南投市"
                                },
                                {
                                    "id": 712821,
                                    "label": "埔里镇"
                                },
                                {
                                    "id": 712822,
                                    "label": "草屯镇"
                                },
                                {
                                    "id": 712823,
                                    "label": "竹山镇"
                                },
                                {
                                    "id": 712824,
                                    "label": "集集镇"
                                },
                                {
                                    "id": 712825,
                                    "label": "名间乡"
                                },
                                {
                                    "id": 712826,
                                    "label": "鹿谷乡"
                                },
                                {
                                    "id": 712827,
                                    "label": "中寮乡"
                                },
                                {
                                    "id": 712828,
                                    "label": "鱼池乡"
                                },
                                {
                                    "id": 712829,
                                    "label": "国姓乡"
                                },
                                {
                                    "id": 712830,
                                    "label": "水里乡"
                                },
                                {
                                    "id": 712831,
                                    "label": "信义乡"
                                },
                                {
                                    "id": 712832,
                                    "label": "仁爱乡"
                                }
                            ],
                            "id": 712800
                        },
                        {
                            "label": "基隆市",
                            "children": [
                                {
                                    "id": 710301,
                                    "label": "中正区"
                                },
                                {
                                    "id": 710302,
                                    "label": "七堵区"
                                },
                                {
                                    "id": 710303,
                                    "label": "暖暖区"
                                },
                                {
                                    "id": 710304,
                                    "label": "仁爱区"
                                },
                                {
                                    "id": 710305,
                                    "label": "中山区"
                                },
                                {
                                    "id": 710306,
                                    "label": "安乐区"
                                },
                                {
                                    "id": 710307,
                                    "label": "信义区"
                                }
                            ],
                            "id": 710300
                        },
                        {
                            "label": "新竹市",
                            "children": [
                                {
                                    "id": 710601,
                                    "label": "东区"
                                },
                                {
                                    "id": 710602,
                                    "label": "北区"
                                },
                                {
                                    "id": 710603,
                                    "label": "香山区"
                                }
                            ],
                            "id": 710600
                        },
                        {
                            "label": "嘉义市",
                            "children": [
                                {
                                    "id": 710701,
                                    "label": "东区"
                                },
                                {
                                    "id": 710702,
                                    "label": "西区"
                                }
                            ],
                            "id": 710700
                        },
                        {
                            "label": "新北市",
                            "children": [
                                {
                                    "id": 710801,
                                    "label": "板桥区"
                                },
                                {
                                    "id": 710802,
                                    "label": "三重区"
                                },
                                {
                                    "id": 710803,
                                    "label": "中和区"
                                },
                                {
                                    "id": 710804,
                                    "label": "永和区"
                                },
                                {
                                    "id": 710805,
                                    "label": "新庄区"
                                },
                                {
                                    "id": 710806,
                                    "label": "新店区"
                                },
                                {
                                    "id": 710807,
                                    "label": "树林区"
                                },
                                {
                                    "id": 710808,
                                    "label": "莺歌区"
                                },
                                {
                                    "id": 710809,
                                    "label": "三峡区"
                                },
                                {
                                    "id": 710810,
                                    "label": "淡水区"
                                },
                                {
                                    "id": 710811,
                                    "label": "汐止区"
                                },
                                {
                                    "id": 710812,
                                    "label": "瑞芳区"
                                },
                                {
                                    "id": 710813,
                                    "label": "土城区"
                                },
                                {
                                    "id": 710814,
                                    "label": "芦洲区"
                                },
                                {
                                    "id": 710815,
                                    "label": "五股区"
                                },
                                {
                                    "id": 710816,
                                    "label": "泰山区"
                                },
                                {
                                    "id": 710817,
                                    "label": "林口区"
                                },
                                {
                                    "id": 710818,
                                    "label": "深坑区"
                                },
                                {
                                    "id": 710819,
                                    "label": "石碇区"
                                },
                                {
                                    "id": 710820,
                                    "label": "坪林区"
                                },
                                {
                                    "id": 710821,
                                    "label": "三芝区"
                                },
                                {
                                    "id": 710822,
                                    "label": "石门区"
                                },
                                {
                                    "id": 710823,
                                    "label": "八里区"
                                },
                                {
                                    "id": 710824,
                                    "label": "平溪区"
                                },
                                {
                                    "id": 710825,
                                    "label": "双溪区"
                                },
                                {
                                    "id": 710826,
                                    "label": "贡寮区"
                                },
                                {
                                    "id": 710827,
                                    "label": "金山区"
                                },
                                {
                                    "id": 710828,
                                    "label": "万里区"
                                },
                                {
                                    "id": 710829,
                                    "label": "乌来区"
                                }
                            ],
                            "id": 710800
                        },
                        {
                            "label": "宜兰县",
                            "children": [
                                {
                                    "id": 712201,
                                    "label": "宜兰市"
                                },
                                {
                                    "id": 712221,
                                    "label": "罗东镇"
                                },
                                {
                                    "id": 712222,
                                    "label": "苏澳镇"
                                },
                                {
                                    "id": 712223,
                                    "label": "头城镇"
                                },
                                {
                                    "id": 712224,
                                    "label": "礁溪乡"
                                },
                                {
                                    "id": 712225,
                                    "label": "壮围乡"
                                },
                                {
                                    "id": 712226,
                                    "label": "员山乡"
                                },
                                {
                                    "id": 712227,
                                    "label": "冬山乡"
                                },
                                {
                                    "id": 712228,
                                    "label": "五结乡"
                                },
                                {
                                    "id": 712229,
                                    "label": "三星乡"
                                },
                                {
                                    "id": 712230,
                                    "label": "大同乡"
                                },
                                {
                                    "id": 712231,
                                    "label": "南澳乡"
                                }
                            ],
                            "id": 712200
                        },
                        {
                            "label": "新竹县",
                            "children": [
                                {
                                    "id": 712401,
                                    "label": "竹北市"
                                },
                                {
                                    "id": 712421,
                                    "label": "竹东镇"
                                },
                                {
                                    "id": 712422,
                                    "label": "新埔镇"
                                },
                                {
                                    "id": 712423,
                                    "label": "关西镇"
                                },
                                {
                                    "id": 712424,
                                    "label": "湖口乡"
                                },
                                {
                                    "id": 712425,
                                    "label": "新丰乡"
                                },
                                {
                                    "id": 712426,
                                    "label": "芎林乡"
                                },
                                {
                                    "id": 712427,
                                    "label": "横山乡"
                                },
                                {
                                    "id": 712428,
                                    "label": "北埔乡"
                                },
                                {
                                    "id": 712429,
                                    "label": "宝山乡"
                                },
                                {
                                    "id": 712430,
                                    "label": "峨眉乡"
                                },
                                {
                                    "id": 712431,
                                    "label": "尖石乡"
                                },
                                {
                                    "id": 712432,
                                    "label": "五峰乡"
                                }
                            ],
                            "id": 712400
                        },
                        {
                            "label": "桃园市",
                            "children": [
                                {
                                    "id": 712301,
                                    "label": "桃园市"
                                },
                                {
                                    "id": 712302,
                                    "label": "中坜市"
                                },
                                {
                                    "id": 712303,
                                    "label": "平镇市"
                                },
                                {
                                    "id": 712304,
                                    "label": "八德市"
                                },
                                {
                                    "id": 712305,
                                    "label": "杨梅市"
                                },
                                {
                                    "id": 712306,
                                    "label": "芦竹市"
                                },
                                {
                                    "id": 712321,
                                    "label": "大溪镇"
                                },
                                {
                                    "id": 712324,
                                    "label": "大园乡"
                                },
                                {
                                    "id": 712325,
                                    "label": "龟山乡"
                                },
                                {
                                    "id": 712327,
                                    "label": "龙潭乡"
                                },
                                {
                                    "id": 712329,
                                    "label": "新屋乡"
                                },
                                {
                                    "id": 712330,
                                    "label": "观音乡"
                                },
                                {
                                    "id": 712331,
                                    "label": "复兴乡"
                                }
                            ],
                            "id": 712300
                        },
                        {
                            "label": "苗栗县",
                            "children": [
                                {
                                    "id": 712501,
                                    "label": "苗栗市"
                                },
                                {
                                    "id": 712521,
                                    "label": "苑里镇"
                                },
                                {
                                    "id": 712522,
                                    "label": "通霄镇"
                                },
                                {
                                    "id": 712523,
                                    "label": "竹南镇"
                                },
                                {
                                    "id": 712524,
                                    "label": "头份镇"
                                },
                                {
                                    "id": 712525,
                                    "label": "后龙镇"
                                },
                                {
                                    "id": 712526,
                                    "label": "卓兰镇"
                                },
                                {
                                    "id": 712527,
                                    "label": "大湖乡"
                                },
                                {
                                    "id": 712528,
                                    "label": "公馆乡"
                                },
                                {
                                    "id": 712529,
                                    "label": "铜锣乡"
                                },
                                {
                                    "id": 712530,
                                    "label": "南庄乡"
                                },
                                {
                                    "id": 712531,
                                    "label": "头屋乡"
                                },
                                {
                                    "id": 712532,
                                    "label": "三义乡"
                                },
                                {
                                    "id": 712533,
                                    "label": "西湖乡"
                                },
                                {
                                    "id": 712534,
                                    "label": "造桥乡"
                                },
                                {
                                    "id": 712535,
                                    "label": "三湾乡"
                                },
                                {
                                    "id": 712536,
                                    "label": "狮潭乡"
                                },
                                {
                                    "id": 712537,
                                    "label": "泰安乡"
                                }
                            ],
                            "id": 712500
                        },
                        {
                            "label": "彰化县",
                            "children": [
                                {
                                    "id": 712701,
                                    "label": "彰化市"
                                },
                                {
                                    "id": 712721,
                                    "label": "鹿港镇"
                                },
                                {
                                    "id": 712722,
                                    "label": "和美镇"
                                },
                                {
                                    "id": 712723,
                                    "label": "线西乡"
                                },
                                {
                                    "id": 712724,
                                    "label": "伸港乡"
                                },
                                {
                                    "id": 712725,
                                    "label": "福兴乡"
                                },
                                {
                                    "id": 712726,
                                    "label": "秀水乡"
                                },
                                {
                                    "id": 712727,
                                    "label": "花坛乡"
                                },
                                {
                                    "id": 712728,
                                    "label": "芬园乡"
                                },
                                {
                                    "id": 712729,
                                    "label": "员林镇"
                                },
                                {
                                    "id": 712730,
                                    "label": "溪湖镇"
                                },
                                {
                                    "id": 712731,
                                    "label": "田中镇"
                                },
                                {
                                    "id": 712732,
                                    "label": "大村乡"
                                },
                                {
                                    "id": 712733,
                                    "label": "埔盐乡"
                                },
                                {
                                    "id": 712734,
                                    "label": "埔心乡"
                                },
                                {
                                    "id": 712735,
                                    "label": "永靖乡"
                                },
                                {
                                    "id": 712736,
                                    "label": "社头乡"
                                },
                                {
                                    "id": 712737,
                                    "label": "二水乡"
                                },
                                {
                                    "id": 712738,
                                    "label": "北斗镇"
                                },
                                {
                                    "id": 712739,
                                    "label": "二林镇"
                                },
                                {
                                    "id": 712740,
                                    "label": "田尾乡"
                                },
                                {
                                    "id": 712741,
                                    "label": "埤头乡"
                                },
                                {
                                    "id": 712742,
                                    "label": "芳苑乡"
                                },
                                {
                                    "id": 712743,
                                    "label": "大城乡"
                                },
                                {
                                    "id": 712744,
                                    "label": "竹塘乡"
                                },
                                {
                                    "id": 712745,
                                    "label": "溪州乡"
                                }
                            ],
                            "id": 712700
                        },
                        {
                            "label": "嘉义县",
                            "children": [
                                {
                                    "id": 713001,
                                    "label": "太保市"
                                },
                                {
                                    "id": 713002,
                                    "label": "朴子市"
                                },
                                {
                                    "id": 713023,
                                    "label": "布袋镇"
                                },
                                {
                                    "id": 713024,
                                    "label": "大林镇"
                                },
                                {
                                    "id": 713025,
                                    "label": "民雄乡"
                                },
                                {
                                    "id": 713026,
                                    "label": "溪口乡"
                                },
                                {
                                    "id": 713027,
                                    "label": "新港乡"
                                },
                                {
                                    "id": 713028,
                                    "label": "六脚乡"
                                },
                                {
                                    "id": 713029,
                                    "label": "东石乡"
                                },
                                {
                                    "id": 713030,
                                    "label": "义竹乡"
                                },
                                {
                                    "id": 713031,
                                    "label": "鹿草乡"
                                },
                                {
                                    "id": 713032,
                                    "label": "水上乡"
                                },
                                {
                                    "id": 713033,
                                    "label": "中埔乡"
                                },
                                {
                                    "id": 713034,
                                    "label": "竹崎乡"
                                },
                                {
                                    "id": 713035,
                                    "label": "梅山乡"
                                },
                                {
                                    "id": 713036,
                                    "label": "番路乡"
                                },
                                {
                                    "id": 713037,
                                    "label": "大埔乡"
                                },
                                {
                                    "id": 713038,
                                    "label": "阿里山乡"
                                }
                            ],
                            "id": 713000
                        },
                        {
                            "label": "云林县",
                            "children": [
                                {
                                    "id": 712901,
                                    "label": "斗六市"
                                },
                                {
                                    "id": 712921,
                                    "label": "斗南镇"
                                },
                                {
                                    "id": 712922,
                                    "label": "虎尾镇"
                                },
                                {
                                    "id": 712923,
                                    "label": "西螺镇"
                                },
                                {
                                    "id": 712924,
                                    "label": "土库镇"
                                },
                                {
                                    "id": 712925,
                                    "label": "北港镇"
                                },
                                {
                                    "id": 712926,
                                    "label": "古坑乡"
                                },
                                {
                                    "id": 712927,
                                    "label": "大埤乡"
                                },
                                {
                                    "id": 712928,
                                    "label": "莿桐乡"
                                },
                                {
                                    "id": 712929,
                                    "label": "林内乡"
                                },
                                {
                                    "id": 712930,
                                    "label": "二仑乡"
                                },
                                {
                                    "id": 712931,
                                    "label": "仑背乡"
                                },
                                {
                                    "id": 712932,
                                    "label": "麦寮乡"
                                },
                                {
                                    "id": 712933,
                                    "label": "东势乡"
                                },
                                {
                                    "id": 712934,
                                    "label": "褒忠乡"
                                },
                                {
                                    "id": 712935,
                                    "label": "台西乡"
                                },
                                {
                                    "id": 712936,
                                    "label": "元长乡"
                                },
                                {
                                    "id": 712937,
                                    "label": "四湖乡"
                                },
                                {
                                    "id": 712938,
                                    "label": "口湖乡"
                                },
                                {
                                    "id": 712939,
                                    "label": "水林乡"
                                }
                            ],
                            "id": 712900
                        },
                        {
                            "label": "屏东县",
                            "children": [
                                {
                                    "id": 713301,
                                    "label": "屏东市"
                                },
                                {
                                    "id": 713321,
                                    "label": "潮州镇"
                                },
                                {
                                    "id": 713322,
                                    "label": "东港镇"
                                },
                                {
                                    "id": 713323,
                                    "label": "恒春镇"
                                },
                                {
                                    "id": 713324,
                                    "label": "万丹乡"
                                },
                                {
                                    "id": 713325,
                                    "label": "长治乡"
                                },
                                {
                                    "id": 713326,
                                    "label": "麟洛乡"
                                },
                                {
                                    "id": 713327,
                                    "label": "九如乡"
                                },
                                {
                                    "id": 713328,
                                    "label": "里港乡"
                                },
                                {
                                    "id": 713329,
                                    "label": "盐埔乡"
                                },
                                {
                                    "id": 713330,
                                    "label": "高树乡"
                                },
                                {
                                    "id": 713331,
                                    "label": "万峦乡"
                                },
                                {
                                    "id": 713332,
                                    "label": "内埔乡"
                                },
                                {
                                    "id": 713333,
                                    "label": "竹田乡"
                                },
                                {
                                    "id": 713334,
                                    "label": "新埤乡"
                                },
                                {
                                    "id": 713335,
                                    "label": "枋寮乡"
                                },
                                {
                                    "id": 713336,
                                    "label": "新园乡"
                                },
                                {
                                    "id": 713337,
                                    "label": "崁顶乡"
                                },
                                {
                                    "id": 713338,
                                    "label": "林边乡"
                                },
                                {
                                    "id": 713339,
                                    "label": "南州乡"
                                },
                                {
                                    "id": 713340,
                                    "label": "佳冬乡"
                                },
                                {
                                    "id": 713341,
                                    "label": "琉球乡"
                                },
                                {
                                    "id": 713342,
                                    "label": "车城乡"
                                },
                                {
                                    "id": 713343,
                                    "label": "满州乡"
                                },
                                {
                                    "id": 713344,
                                    "label": "枋山乡"
                                },
                                {
                                    "id": 713345,
                                    "label": "三地门乡"
                                },
                                {
                                    "id": 713346,
                                    "label": "雾台乡"
                                },
                                {
                                    "id": 713347,
                                    "label": "玛家乡"
                                },
                                {
                                    "id": 713348,
                                    "label": "泰武乡"
                                },
                                {
                                    "id": 713349,
                                    "label": "来义乡"
                                },
                                {
                                    "id": 713350,
                                    "label": "春日乡"
                                },
                                {
                                    "id": 713351,
                                    "label": "狮子乡"
                                },
                                {
                                    "id": 713352,
                                    "label": "牡丹乡"
                                }
                            ],
                            "id": 713300
                        },
                        {
                            "label": "台东县",
                            "children": [
                                {
                                    "id": 713401,
                                    "label": "台东市"
                                },
                                {
                                    "id": 713421,
                                    "label": "成功镇"
                                },
                                {
                                    "id": 713422,
                                    "label": "关山镇"
                                },
                                {
                                    "id": 713423,
                                    "label": "卑南乡"
                                },
                                {
                                    "id": 713424,
                                    "label": "鹿野乡"
                                },
                                {
                                    "id": 713425,
                                    "label": "池上乡"
                                },
                                {
                                    "id": 713426,
                                    "label": "东河乡"
                                },
                                {
                                    "id": 713427,
                                    "label": "长滨乡"
                                },
                                {
                                    "id": 713428,
                                    "label": "太麻里乡"
                                },
                                {
                                    "id": 713429,
                                    "label": "大武乡"
                                },
                                {
                                    "id": 713430,
                                    "label": "绿岛乡"
                                },
                                {
                                    "id": 713431,
                                    "label": "海端乡"
                                },
                                {
                                    "id": 713432,
                                    "label": "延平乡"
                                },
                                {
                                    "id": 713433,
                                    "label": "金峰乡"
                                },
                                {
                                    "id": 713434,
                                    "label": "达仁乡"
                                },
                                {
                                    "id": 713435,
                                    "label": "兰屿乡"
                                }
                            ],
                            "id": 713400
                        },
                        {
                            "label": "花莲县",
                            "children": [
                                {
                                    "id": 713501,
                                    "label": "花莲市"
                                },
                                {
                                    "id": 713521,
                                    "label": "凤林镇"
                                },
                                {
                                    "id": 713522,
                                    "label": "玉里镇"
                                },
                                {
                                    "id": 713523,
                                    "label": "新城乡"
                                },
                                {
                                    "id": 713524,
                                    "label": "吉安乡"
                                },
                                {
                                    "id": 713525,
                                    "label": "寿丰乡"
                                },
                                {
                                    "id": 713526,
                                    "label": "光复乡"
                                },
                                {
                                    "id": 713527,
                                    "label": "丰滨乡"
                                },
                                {
                                    "id": 713528,
                                    "label": "瑞穗乡"
                                },
                                {
                                    "id": 713529,
                                    "label": "富里乡"
                                },
                                {
                                    "id": 713530,
                                    "label": "秀林乡"
                                },
                                {
                                    "id": 713531,
                                    "label": "万荣乡"
                                },
                                {
                                    "id": 713532,
                                    "label": "卓溪乡"
                                }
                            ],
                            "id": 713500
                        },
                        {
                            "label": "澎湖县",
                            "children": [
                                {
                                    "id": 713601,
                                    "label": "马公市"
                                },
                                {
                                    "id": 713621,
                                    "label": "湖西乡"
                                },
                                {
                                    "id": 713622,
                                    "label": "白沙乡"
                                },
                                {
                                    "id": 713623,
                                    "label": "西屿乡"
                                },
                                {
                                    "id": 713624,
                                    "label": "望安乡"
                                },
                                {
                                    "id": 713625,
                                    "label": "七美乡"
                                }
                            ],
                            "id": 713600
                        }
                    ],
                    "label": "台湾省",
                    "id": 710000
                },
                {
                    "children": [],
                    "label": "钓鱼岛",
                    "id": 900000
                },
                {
                    "children": [
                        {
                            "label": "澳门特别行政区",
                            "children": [
                                {
                                    "id": 820101,
                                    "label": "澳门半岛"
                                },
                                {
                                    "id": 820102,
                                    "label": "凼仔"
                                },
                                {
                                    "id": 820103,
                                    "label": "路凼城"
                                },
                                {
                                    "id": 820104,
                                    "label": "路环"
                                }
                            ],
                            "id": 820100
                        }
                    ],
                    "label": "澳门特别行政区",
                    "id": 820000
                },
                {
                    "children": [
                        {
                            "label": "香港特别行政区",
                            "children": [
                                {
                                    "id": 810102,
                                    "label": "中西区"
                                },
                                {
                                    "id": 810103,
                                    "label": "东区"
                                },
                                {
                                    "id": 810104,
                                    "label": "九龙城区"
                                },
                                {
                                    "id": 810105,
                                    "label": "观塘区"
                                },
                                {
                                    "id": 810106,
                                    "label": "南区"
                                },
                                {
                                    "id": 810107,
                                    "label": "深水埗区"
                                },
                                {
                                    "id": 810108,
                                    "label": "湾仔区"
                                },
                                {
                                    "id": 810109,
                                    "label": "黄大仙区"
                                },
                                {
                                    "id": 810110,
                                    "label": "油尖旺区"
                                },
                                {
                                    "id": 810111,
                                    "label": "离岛区"
                                },
                                {
                                    "id": 810112,
                                    "label": "葵青区"
                                },
                                {
                                    "id": 810113,
                                    "label": "北区"
                                },
                                {
                                    "id": 810114,
                                    "label": "西贡区"
                                },
                                {
                                    "id": 810115,
                                    "label": "沙田区"
                                },
                                {
                                    "id": 810116,
                                    "label": "屯门区"
                                },
                                {
                                    "id": 810117,
                                    "label": "大埔区"
                                },
                                {
                                    "id": 810118,
                                    "label": "荃湾区"
                                },
                                {
                                    "id": 810119,
                                    "label": "元朗区"
                                }
                            ],
                            "id": 810100
                        }
                    ],
                    "label": "香港特别行政区",
                    "id": 810000
                }
            ], // 港澳台地区
            oldData: {
                provinceList: [],
                cityList: [],
                areaList: []
            }, //旧数据
        }
    },
    mounted() {
        
        
    },
    methods:{
        handleChange(v){
            console.log(v)
        },
        handlerFilterStr(){
            this.jsonStr = JSON.stringify(this.originData)
        },
        handlerFilterSpace(){
            let reg = RegExp(' ', "g")
            let s = this.originData.replace(reg, '')
            this.jsonStr = JSON.stringify(s)
        },
        handlerFilterTab(){
            let reg = RegExp('\t', "g")
            let s = this.originData.replace(reg, '')
            this.jsonStr = JSON.stringify(s)
        },
        /**
         * 获取全部省一级数据
         * @date 2021-11-19
         * @param {any} list
         * @returns {any} 返回第一级(省/自治区/直辖市)
         */
        handlerProvince(list){
            let provinceList = []
            list.map(i => {
                let administrativeCode = i.substr(0,6)
                let administrativeName = i.substr(6)
                
                let administrativeCodeStr = administrativeCode.toString()
                // 省(后四位是'0000')
                if(administrativeCodeStr.endsWith('0000')){
                    // console.log('省：',administrativeCode,administrativeName)
                    let obj = null
                    
                    obj = {
                        id: administrativeCode,
                        label: administrativeName,
                        children: []
                    }
                    if(administrativeName.endsWith('市')){
                        // 直辖市
                        provinceList.push(obj)
                    }else{
                        // 省
                        provinceList.push(obj)
                    }
                    
                }
            })
            return provinceList
        },
        /**
         * 获取全部二级数据(市、xx自治州、xx地区、xx盟)
         * @date 2021-11-19
         * @param {any} list
         * @returns {any} 返回第一级(省/自治区/直辖市) ['130100石家庄市', '130200唐山市', ...]
         */
        handlerCity(list){
            let cityList = []
            list.map(i => {
                let administrativeCode = i.substr(0,6)
                let administrativeName = i.substr(6)
                let administrativeCodeStr = administrativeCode.toString()
                // 市(第3、4位不是'00' && 后两位是'00')
                if(administrativeCodeStr.substr(2,2) != '00' && administrativeCodeStr.endsWith('00')){
                    let obj = null
                    
                    obj = {
                        id: administrativeCode,
                        label: administrativeName,
                        children: []
                    }
                    cityList.push(obj)
                }
                // 直辖市 后4位是'0000' && 最后一个字符是'市'
                if(administrativeCodeStr.endsWith('0000') && administrativeName.endsWith('市')){
                    // 计算直辖市的代码
                    let provinceCode2 = administrativeCodeStr.substr(0,2)
                    let cityId = `${provinceCode2}0100`
                    
                    cityList.push({
                        id: cityId,
                        label: administrativeName,
                        children: []
                    })
                }
            })
            // 自定义直辖数据
            let obj = null
            obj = [
                {
                    id: '659000',
                    label: '自治区直辖县级行政单位', // 新疆
                    children: []
                },
                {
                    id: '469000',
                    label: '省直辖县级行政单位', // 海南
                    children: []
                },
                {
                    id: '419000',
                    label: '省直辖县级行政单位', // 河南
                    children: []
                },
                {
                    id: '429000',
                    label: '省直辖县级行政单位', // 湖北
                    children: []
                }
            ]
            cityList.push(...obj)
            return cityList
        },
        /**
         * 获取全部三级数据(区/县/自治县/县级市)
         * @date 2021-11-19
         * @param {any} list
         * @returns {any} 返回第一级(省/自治区/直辖市)
         */
        handlerArea(list){
            let areaList = []
            list.map(i => {
                let administrativeCode = i.substr(0,6)
                
                let administrativeCodeStr = administrativeCode.toString()
                // 区县(后两位不是'00')
                if(administrativeCodeStr.substr(4,2) != '00'){
                    areaList.push(i)
                }
            })
            return areaList
        },
        /**
         * 省市匹配
         * @date 2021-11-19
         * @param {any} cityList
         * @param {any} provinceList
         * @returns {any} 返回第一级(省/自治区/直辖市) 
         */
        handlerProvinceCity(cityList, provinceList){
            let result = []
            // 省市匹配(一级与二级匹配)
            cityList.map(item => {
                
                let cityCode3 = item.id.substr(0,3) // 城市代码前3位
                provinceList.map(p => {
                    let provinceCodeStr = p.id.toString()

                    // 市属于哪个省
                    let cityFilter1 = provinceCodeStr.substr(0,2) == item.id.substr(0,2) 
                    // 内蒙古 与 xx盟的关系匹配
                    let cityFilter2 = provinceCodeStr.substr(0, 2) == '15' && item.id.substr(0, 2) == '15' //item.label.endsWith('盟')
                    // 新疆 与 xx自治州、xx地区的关系匹配
                    let cityFilter3 = provinceCodeStr.substr(0, 2) == '65' && item.id.substr(0, 2) == '65' // (item.label.endsWith('自治州') || item.label.endsWith('地区'))
                    // 青海 与 xx自治州、xx地区的关系匹配
                    let cityFilter4 = provinceCodeStr.substr(0, 2) == '63' && item.id.substr(0, 2) == '63' // item.label.endsWith('自治州')
                    
                    // 海南省直辖市匹配
                    let cityFilter6 = provinceCodeStr.substr(0, 2) == '46' && cityCode3 == '469'
                    // 河南直辖市匹配
                    let cityFilter7 = provinceCodeStr.substr(0, 2) == '41' && cityCode3 == '419'
                    // 湖北直辖市匹配
                    let cityFilter8 = provinceCodeStr.substr(0, 2) == '42' && cityCode3 == '429'

                    if(cityFilter1 || cityFilter2 || cityFilter3 || cityFilter4 || cityFilter6 || cityFilter7 || cityFilter8){
                        p.children.push(item)
                    }
                })
                
            })
            result = provinceList
            return result
        },
        
        /**
         * 市区匹配
         * 市区匹配完毕后，检查市下面有没有区，如果一个都没有，需要单独处理，如：东莞市，中山市
         * 
         * @date 2021-11-19
         * @param {any} provinceList
         * @param {any} cityList
         * @returns {any} 返回第一级(省/自治区/直辖市) 
         */
        handlerCityArea(cityList, areaList){
            let cityAreaList = []
            areaList.map(item => {
                let administrativeCode = item.substr(0,6)
                let administrativeName = item.substr(6)
                
                let administrativeCodeStr = administrativeCode.toString()
                let areaCode3 = administrativeCodeStr.substr(0,3) // 区代码前3位
                let areaCode4 = administrativeCodeStr.substr(0,4) // 区代码前4位
                
                cityList.map(c => {
                    // console.log(1962,c,c.cityId)
                    let cityCodeStr = c.id.toString()
                    let obj = null
                    obj = {
                        id: administrativeCode,
                        label: administrativeName
                    }
                    // 区县属于哪个市
                    if(c.id.substr(0,4) == areaCode4){
                        c.children.push(obj)
                    }
                    // 海南直辖市
                    if(areaCode3 == '469'){
                        if(cityCodeStr.substr(0,3) == areaCode3){
                            let searchResult = c.children.some(s => s.id == administrativeCode)
                            if(!searchResult) {
                                c.children.push(obj)
                            }
                        }
                    }
                    
                    // 重庆市单独处理（重庆的区和县规则不一致，区和县第4位代码不同）
                    if(areaCode3 == '500'){
                        if(cityCodeStr.substr(0,3) == areaCode3){
                            let searchResult = c.children.some(s => s.id == administrativeCode)
                            if(!searchResult) {
                                c.children.push(obj)
                            }
                            
                        }
                    }
                    // 新疆 自治区直辖县级行政单位
                    if(areaCode3 == '659'){
                        if(cityCodeStr.substr(0,3) == areaCode3){
                            let searchResult = c.children.some(s => s.id == administrativeCode)
                            if(!searchResult) {
                                c.children.push(obj)
                            }
                        }
                    }
                })
            })
            cityAreaList = cityList
            // 处理没有区/县的城市（把市直接添加到children）
            let cityN = cityAreaList.filter(item => item.children.length == 0)
            console.log('没有区/县的城市', cityN)
            cityN.map(item => {
                item.children.push({
                    id: item.id,
                    label: item.label
                })
            })
            console.log('处理没有区/县的城市', cityN)
            return cityAreaList
        },
        handlerEl(){
            // console.log(this.originData)
            // \t:标识空四个字符，想当于缩进，Tab键
            if(this.originData == "") return false
            console.time('过滤时间')
            let reg = RegExp(' ', "g")
            let filterSpaceResult = this.originData.replace(reg, '')

            let regT = RegExp('\t', "g")
            let filterTab = filterSpaceResult.replace(regT, '')
            let list = filterTab.split(`\n`) // ['110000北京市', '110101东城区', '110102西城区'...]
            console.timeEnd('过滤时间')
            console.log(list)

            let resultList = [] // 省市区三级
            console.time('获取一级时间')
            resultList = this.handlerProvince(list)
            console.timeEnd('获取一级时间')
            console.log(`一级(省/自治区/直辖市/特区)${resultList.length}`,resultList)
            
            let allCityList = []
            console.time('获取二级时间')
            allCityList = this.handlerCity(list)
            console.timeEnd('获取二级时间')
            console.log(`二级(市/县/xx自治州/xx地区/xx盟)${allCityList.length}`,allCityList)
            
            let allAreaList = []
            console.time('获取三级时间')
            allAreaList = this.handlerArea(list)
            console.timeEnd('获取三级时间')
            console.log(`三级(区/县/自治县/县级市/xx旗)${allAreaList.length}`,allAreaList)
            
            console.time('市区匹配时间')
            let cityAreaList = this.handlerCityArea(allCityList, allAreaList)
            console.timeEnd('市区匹配时间')
            console.log('市区', cityAreaList)

            console.time('省市-匹配时间')
            let provinceCityAreaList = this.handlerProvinceCity(cityAreaList, resultList)
            console.timeEnd('省市-匹配时间')
            console.log('省市区最终结果', provinceCityAreaList)
            
            this.resultData = [...provinceCityAreaList, ...this.otherData]
            this.jsonStr = JSON.stringify([...provinceCityAreaList, ...this.otherData])
        }
    }
}
</script>
<style scoped>
.source-data{
    width: 300px;
    margin-right:10px;
}
.result-data{
    width: 500px;
    flex: 1;
}
.operation{
    margin: 10px 0;
}
.remark{
    line-height:20px;
    font-size: 14px;
    color: #999;
}
</style>